<template>
  <div class="dashboard-editor-container">
    
    <el-row>
      <!-- <el-col :span="5">
        <el-row class="panel-group">
          <el-col class="card-panel-col">
            <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
              <div class="card-panel-icon-wrapper icon-people">
                <svg-icon icon-class="peoples" class-name="card-panel-icon" />
              </div>
              <div class="card-panel-description">
                <div class="card-panel-text">商品数量</div>
                <count-to :start-val="0" :end-val="productCount" :duration="2600" class="card-panel-num" />
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row class="panel-group">
          <el-col class="card-panel-col">
            <div class="card-panel" @click="handleSetLineChartData('messages')">
              <div class="card-panel-icon-wrapper icon-message">
                <svg-icon icon-class="message" class-name="card-panel-icon" />
              </div>
              <div class="card-panel-description">
                <div class="card-panel-text">未发货单</div>
                <count-to
                  :start-val="0"
                  :end-val="waitStockCount"
                  :duration="3000"
                  class="card-panel-num"
                />
              </div>
            </div>
          </el-col>
        </el-row>
      </el-col> -->
      <el-col :span="8">
        <div class="topState">
          <div class="roomname">{{ equipmentList[0]?equipmentList[0].name:'暂无机房' }}</div>
          <div v-if="equipmentList[0]&&equipmentList[0].status == '正常'" class="state green">{{ equipmentList[0]?equipmentList[0].status:"" }}</div>
          <div v-if="equipmentList[0]&&equipmentList[0].status == '异常'" class="state red">{{ equipmentList[0]?equipmentList[0].status:"" }}</div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="topState">
          <div class="roomname">{{ equipmentList[1]?equipmentList[1].name:'暂无机房' }}</div>
          <div v-if="equipmentList[1]&&equipmentList[1].status == '正常'" class="state green">{{ equipmentList[1]?equipmentList[1].status:"" }}</div>
          <div v-if="equipmentList[1]&&equipmentList[1].status == '异常'" class="state red">{{ equipmentList[1]?equipmentList[1].status:"" }}</div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="topState">
          <div class="roomname">{{ equipmentList[2]?equipmentList[2].name:'暂无机房' }}</div>
          <div v-if="equipmentList[2]&&equipmentList[2].status == '正常'" class="state green">{{ equipmentList[2]?equipmentList[2].status:"" }}</div>
          <div v-if="equipmentList[2]&&equipmentList[2].status == '异常'" class="state red">{{ equipmentList[2].status }}</div>
        </div>
      </el-col>
    </el-row>
<div class="date_css">
      <el-select v-model="value" style="margin-left:30px" placeholder="请选择" @change="roomChange">
        <el-option
          v-for="item in computerRoomList"
          :key="item.id"
          :label="item.name"
          :value="item.id"/>
      </el-select>
    </div>
    <el-row>
      <!-- <el-col v-for="(item,index) in dataInfo" :span="24" :key="index">
        <div :id="[`sumChart${index}`]" class="chart" style="height: 400px; padding: 30px"/>
      </el-col> -->
      <el-col :span="24" >
        <div id="sumChart" class="chart" style="height: 400px; padding: 30px"/>
        <div id="sumChart1" class="chart" style="height: 400px; padding: 30px"/>
      </el-col>
      <!-- <el-col :span="11">
        <el-row>
          <el-col :span="12">
            <div id="areaChart" class="chart" style="height: 400px; padding: 30px"/>
          </el-col>
          <el-col :span="12">
            <div id="channelChart" class="chart" style="height: 400px; padding: 30px"/>
          </el-col>
        </el-row>
      </el-col> -->
    </el-row>
  </div>
</template>

<script>
import { info, listRoom, computerRoomXiaLa } from '@/api/dashboard'
import CountTo from 'vue-count-to'
import { parseTime } from '@/utils'

var echarts = require('echarts')
export default {
  components: {
    CountTo
  },
  data() {
    return {
      productCount: 0,
      waitStockCount: 0,
      dataInfo: [],
      batteryInfo: [],
      listQuery: {
        page: 1,
        limit: 3
      },
      computerRoomList: [],
      equipmentList: [{
        status: ""
      }, {
        status: ""
      }, {
        status: ""
      }],
      value: '',
      roomid: '',
      y1List: [],
      opinionData: ['3', '2', '4', '4', '5']
    }
  },
  created() {
    this.stateList()
    this.getDate()
    this.xialaInfo()
    this.getData()
  },
  // 调用
  mounted() {
    this.$nextTick(function() {
      // this.getData()
    })
  },
  methods: {
    stateList() {
      listRoom(this.listQuery).then(response => {
        if (response.data.data.items) {
          this.equipmentList = response.data.data.items
        }
      }).catch(() => {})
    },
    getDate() {
      const date = new Date()
      const year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hours = date.getHours()
      let minutes = date.getMinutes()
      let seconds = date.getSeconds() + 1
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      if (hours < 10) {
        hours = '0' + hours
      }
      if (minutes < 10) {
        minutes = '0' + minutes
      }
      if (seconds < 10) {
        seconds = '0' + seconds
      }
      this.specificDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
    },
    xialaInfo() {
      computerRoomXiaLa()
        .then((response) => {
          // 设备类型下拉
          if (response.data.data.length > 0) {
            this.computerRoomList = response.data.data
            this.roomid = this.computerRoomList[0].id
            this.value = this.computerRoomList[0].name
            this.getData()
          }
        })
    },
    roomChange(e) {
      this.roomid = e
      this.getData()
    },
    getData() {
      info({ roomId: this.roomid }).then(response => {
        var _obj = []
        this.dataInfo = []
        if (response.data.data.equipmentTypeList) {
          _obj = response.data.data
          for (let i = 0; i < _obj.equipmentTypeList.length; i++) { // 循环设备类型
            this.dataInfo.push({
              name: '',
              equipmentNo: [], // x轴
              temperatureOne: [], // y轴   上轮温度
              temperatureTwo: [], // y轴   本轮温度
              humidity: [] // y轴   湿度
            })
            this.dataInfo[i].name = _obj.equipmentTypeList[i].equipmentType
            for (let j = 0; j < _obj.equipmentTypeList[i].equipmentNoList.length; j++) { // 循环设备编号集合
              this.dataInfo[i].equipmentNo.push(_obj.equipmentTypeList[i].equipmentNoList[j].equipmentNo) // 编号
              if (_obj.equipmentTypeList[i].equipmentNoList[j].inspectionDOList) {
                for (let n = 0; n < _obj.equipmentTypeList[i].equipmentNoList[j].inspectionDOList.length; n++) { // 循环巡检记录
                  if (n === 0) {
                    this.dataInfo[i].temperatureOne.push(_obj.equipmentTypeList[i].equipmentNoList[j].inspectionDOList[n].temperature) // 温度
                  } else if (n === 1) {
                    this.dataInfo[i].temperatureTwo.push(_obj.equipmentTypeList[i].equipmentNoList[j].inspectionDOList[n].temperature) // 温度
                  }
                  // this.dataInfo[i].humidity.push(_obj.equipmentTypeList[i].equipmentNoList[j].inspectionDOList[n].humidity) //湿度
                }
              }
            }
          }
        } else {
          _obj = []
        }

        if (response.data.data.batteryList) {
          _obj = response.data.data
          for (let i = 0; i < _obj.batteryList.length; i++) { // 循环设备类型
            this.batteryInfo.push({
              name: '',
              equipmentNo: [], // x轴
              temperatureOne: [], // y轴   上轮温度
              temperatureTwo: [], // y轴   本轮温度
              humidity: [] // y轴   湿度
            })
            this.batteryInfo[i].equipmentNo = []
            this.batteryInfo[i].name = _obj.batteryList[i].equipmentType
            for (let j = 0; j < _obj.batteryList[i].batteryInspectionList.length; j++) { // 循环设备编号集合
              this.batteryInfo[i].equipmentNo.push(parseTime(_obj.batteryList[i].batteryInspectionList[j].gmtCreate,'{m}-{d} {h}:{i}')) // 编号
              if (_obj.batteryList[i].batteryInspectionList[j]) {
                    this.batteryInfo[i].temperatureOne.push(_obj.batteryList[i].batteryInspectionList[j].temperature) // 温度
              }
            }
          }
        } else {
          _obj = []
        }


        // const orderChart = echarts.init(document.getElementById('orderChart'))
        // const areaChart = echarts.init(document.getElementById('areaChart'))
        // const channelChart = echarts.init(document.getElementById('channelChart'))
        this.waitStockCount = response.data.data.waitStockCount
        this.productCount = response.data.data.goodsCount

        // 每日订单走势
        this.charts = echarts.init(document.getElementById('sumChart'))
        this.charts.setOption({
          title: { text: this.dataInfo.length > 0 ? this.dataInfo[0].name+'温度监控' : [] },
          tooltip: {
            trigger: 'axis'
          },
          color: ['green', 'blue'],
          legend: {
            data: ['本轮', '上一轮']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },

          toolbox: {
            feature: {
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.dataInfo.length > 0 ? this.dataInfo[0].equipmentNo : []

          },
          yAxis: {
            type: 'value'
          },

          series: [{
            name: '上一轮',
            type: 'line',
            stack: '总量',
            data: this.dataInfo.length > 0 ? this.dataInfo[0].temperatureOne : [],
            lineStyle: {
              color: 'green'
            }

          },
          {
            name: '本轮',
            type: 'line',
            stack: '总量1',
            data: this.dataInfo.length > 0 ? this.dataInfo[0].temperatureTwo : [],
            lineStyle: {
              color: 'blue'
            }
          }]
        })
        this.charts1 = echarts.init(document.getElementById('sumChart1'))
        this.charts1.setOption({
          title: { text: this.batteryInfo.length > 0 ? this.batteryInfo[0].name+'温度监控' : [] },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: []
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },

          toolbox: {
            feature: {
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.batteryInfo.length > 0 ? this.batteryInfo[0].equipmentNo : []

          },
          yAxis: {
            type: 'value'
          },

          series: [{
            name: '',
            type: 'line',
            stack: '',
            data: this.batteryInfo.length > 0 ? this.batteryInfo[0].temperatureOne : [],
            lineStyle: {
              color: 'green'
            }

          }]
        })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: #ffffff;
  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

.panel-group {
  margin-top: 18px;
  padding-left: 30px;

  .card-panel-col {
    margin-bottom: 32px;
  }
  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);
    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }
      .icon-people {
        background: #40c9c6;
      }
      .icon-message {
        background: #36a3f7;
      }
      .icon-money {
        background: #f4516c;
      }
      .icon-shoppingCard {
        background: #34bfa3;
      }
    }
    .icon-people {
      color: #40c9c6;
    }
    .icon-message {
      color: #36a3f7;
    }
    .icon-money {
      color: #f4516c;
    }
    .icon-shoppingCard {
      color: #34bfa3;
    }
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }
    .card-panel-icon {
      float: left;
      font-size: 48px;
    }
    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;
      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }
      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}
.date_css{
margin-left: 50px;
}
.topState{
  width: 100%;
  padding: 30px;
  text-align: center;
  margin-bottom: 30px;
  .roomname{
    margin-bottom: 30px;
  }
  .state{
    font-size: 50px;
  }
  .green{
    color: #0eb628;
  }
  .red{
    color: #cd0000;
  }

}
</style>
